﻿using System;
using System.Runtime.InteropServices;
using System.Windows;
using Co9320.NamaAlert.Extension.ViewModel;

namespace Co9320.NamaAlert.Extension.View
{
    /// <summary>
    /// OptionWindow.xaml の相互作用ロジック
    /// </summary>
    partial class OptionWindow : Window
    {
        public OptionWindow()
        {
            InitializeComponent();
        }

        private void okButton_Click(object sender, RoutedEventArgs e)
        {
            OptionViewModel context = (OptionViewModel)this.DataContext;
            this.DialogResult = true;
            this.Close();
            context.Save();
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void Window_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            OptionViewModel context = (OptionViewModel)this.DataContext;
            IntPtr pass = Marshal.SecureStringToBSTR(context.SmtpPassword);
            this.SmtpPassword.Password = Marshal.PtrToStringBSTR(pass);
        }

        private void SmtpPassword_PasswordChanged(object sender, RoutedEventArgs e)
        {
            OptionViewModel context = (OptionViewModel)this.DataContext;
            context.SmtpPassword.Clear();
            foreach (char pass in this.SmtpPassword.Password.ToCharArray())
                context.SmtpPassword.AppendChar(pass);
        }
    }
}
